package cn.ppool.xmlmapper.builder;

import cn.ppool.xmlmapper.ApplicationConfigure;
import cn.ppool.xmlmapper.builder.parse.Mapper;
import cn.ppool.xmlmapper.utils.VmfResource;
import cn.ppool.xmlmapper.sql.RequestSqlService;
import cn.ppool.xmlmapper.sql.SelectObjectPackerService;
import cn.ppool.xmlmapper.utils.BeanUtil;
import cn.ppool.xmlmapper.worker.ProxyFactory;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.support.*;
import org.springframework.core.io.ResourceLoader;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

@Log4j2
public class MapperBuilder {

    @Getter
    private final RequestSqlService requestSqlService;                        // sql dao底层接口
    @Getter
    private Map<String, Mapper> mapperList;                             // mapper model
    @Getter
    private final SelectObjectPackerService selectObjectPackerService;        // select查询打包类
    @Getter
    private String customerJavascriptFunction;                          // js扩展
    @Getter
    private final ApplicationConfigure applicationConfigure;

    public MapperBuilder(BeanDefinitionRegistry registry,
                         ResourceLoader resourceLoader,
                         ApplicationConfigure applicationConfigure,
                         SelectObjectPackerService selectObjectPackerService,
                         RequestSqlService requestSqlService) throws Exception {
        this.requestSqlService = requestSqlService;
        this.selectObjectPackerService = selectObjectPackerService;
        this.applicationConfigure = applicationConfigure;
        build(registry, resourceLoader);
    }

    public void build(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) throws Exception {
        // 合并js
        StringBuilder js = new StringBuilder();
        List<InputStream> jsFilePath = VmfResource.findResourcesAsInputStream("classpath*:cn/ppool/xmlmapper/**/*.js", resourceLoader);
        for (InputStream jsStream : jsFilePath) {
            byte[] byt = new byte[jsStream.available()];
            jsStream.read(byt);
            String jsContent = new String(byt, StandardCharsets.UTF_8);
            js.append(jsContent).append("\n");
        }
        this.customerJavascriptFunction = js.toString();
        // xml 建模
        this.mapperList = new HashMap<>();
        for (String path : this.applicationConfigure.getMapperScan()) {
            List<InputStream> mapperFilePath = VmfResource.findResourcesAsInputStream(path, resourceLoader);
            for (InputStream xmlPath : mapperFilePath) {
                Mapper map = new Mapper(this);
                map.build(xmlPath);
                this.mapperList.put(map.getNamespace(), map);
            }
        }
        // xml 注册
        for (Map.Entry<String, Mapper> mp : this.mapperList.entrySet()) {
            Mapper map = mp.getValue();
            Map<String, Object> propertyValues = new HashMap<>();
            propertyValues.put("configure", this);
            BeanUtil.registerProxyBean(registry, ProxyFactory.class, map.getProxyClass(), map.getNamespace(), propertyValues);
        }
    }



}
